package com.itmg.tools.mytags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts2.ServletActionContext;

public class PageTag extends TagSupport {
	private static final long serialVersionUID = -1122449622265941160L;
	// action url
	private String action;
	// 当前页码
	private Integer index;
	// 最大页码数
	private Integer maxIndex;
	// 每页最多显示个数

	private Integer size;
	// 每组显示页码个数
	private Integer groupSize;
	// css
	private String styleClass;
	// 模板
	private String theme;

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = ServletActionContext.getRequest ();	
		JspWriter out = pageContext.getOut();
		String keywords = request.getParameter("keywords");
        String category = request.getParameter("category");
        String minPrice = request.getParameter("minPrice");
        String maxPrice = request.getParameter("maxPrice");
        String sort = request.getParameter("sort");
        String isAuction = request.getParameter("isAuction");
        if(isAuction == null)
        	isAuction = "0";
		try {
			if(category != null && !"".equals(category)){
				if(minPrice != null || maxPrice != null)
					action += keywords+"/category/"+category+"/price/"+minPrice+"-"+maxPrice+"/";
				else
					action += keywords+"/category/"+category+"/";
			}else{
				if(minPrice != null || maxPrice != null)
					action += keywords+"/price/"+minPrice+"-"+maxPrice+"/";
				else
					action += keywords+"/";
			}
			StringBuffer html = new StringBuffer();
			String url = null;
			// 如果只有一页，则无需分页
			if (maxIndex > 1) {
				// 数字样式 << prev 1 2 3 4 5 6 7 8 9 10 next >>
				html.append("<div ");
				if (styleClass != null) {
					html.append("class='" + styleClass + "'>");
				} else {
					html.append(">");
				}
				if (index > 1) {
					// 当前不是第一组，要显示"<< Prev"
					// << Prev返回前一页
					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
						url = action + "/"+sort+"/"+isAuction;
					else
						url = action;
					
//					html.append("<a href='" + url + "'>先頭ページへ</a>&nbsp;");
					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
					    url = action + (index - 1) +"/"+sort+"/"+isAuction;
					else
						if(index>2)
							url = action + (index - 1);
						else
							url = action;
					html.append("<a rel=\"nofollow\" href='" + url + "'>前ページへ</a>&nbsp;");
				}
				// 从第begin页开始显示到end页,每组有groupSize个页数

				
				int begin, end;
				// groupSize不为空,分组显示,否则全部显示
				if (groupSize != null && groupSize < maxIndex) {
					// 每组显示页码个数的一半

					int halfGroupSize = (groupSize - 1) / 2;
					begin = (index - (halfGroupSize + 1)) > 0 ? (index - (halfGroupSize + 1)) : 1;
					end = (index + halfGroupSize) < maxIndex ? (index + halfGroupSize) : maxIndex;
					if (end == maxIndex) {
						if (maxIndex - groupSize < 1) {
							begin = maxIndex;
						}
						begin = maxIndex - groupSize;
					} else if (begin == 1 && end < maxIndex) {
						end = maxIndex > groupSize ? groupSize : maxIndex;
					}
				} else {
					begin = 1;
					end = maxIndex;
				}
				// groupSize个为一组显示

				for (int i = begin; i <= end; i++) {
					if (index == i) { // 当前页要加粗显示
						html.append("<span class=\"current\">" + i + "</span>&nbsp;");
					} else {
						if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
							url = action + i + "/"+sort+"/"+isAuction;
						else
							if(i==1)
								url = action;
							else
								url = action + i;
						
						html.append("<a rel=\"nofollow\" href='" + url + "'>" + i + "</a>&nbsp;");
					}
				}
				if (index < maxIndex) {
					// >>：返回下一组最后一页

					// >：返回下一页
					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
						url = action + (index + 1) + "/"+sort+"/"+isAuction;
					else
						url = action + (index + 1);
					
					html.append("<a rel=\"nofollow\" href='" + url + "'>次ページへ</a>&nbsp;");
					
					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
						url = action + maxIndex + "/"+sort+"/"+isAuction;
					else
						url = action + maxIndex;
					
//					html.append("<a href='" + url + "'>最終ページへ</a>");
				}
				html.append("</div>");
			}
			out.write(html.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (SKIP_BODY);
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public Integer getMaxIndex() {
		return maxIndex;
	}

	public void setMaxIndex(Integer maxIndex) {
		this.maxIndex = maxIndex;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Integer getGroupSize() {
		return groupSize;
	}

	public void setGroupSize(Integer groupSize) {
		this.groupSize = groupSize;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

}
